﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 客户端Cookie操作助手类  ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                     ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone                ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Web;
using Wedn.Net.Common;

namespace Wedn.Net.Utility
{
    /// <summary>
    /// 客户端Cookie操作助手类
    /// </summary>
    public class CookieHelper
    {
        /// <summary>
        /// 添加Cookies值
        /// </summary>
        /// <param name="key">该Cookie的键</param>
        /// <param name="value">该Cookie的值</param>
        public static void AddCookie(string key, string value)
        {
            SetCookie(key, value, DateTime.Now.AddDays(7), "/");
        }

        /// <summary>
        /// 设置客户端Cookies值
        /// </summary>
        /// <param name="key">该Cookie的键</param>
        /// <param name="value">该Cookie的值</param>
        public static void SetCookie(string key, string value)
        {
            SetCookie(key, value, DateTime.Now.AddDays(7), "/");
        }

        /// <summary>
        /// 设置客户端Cookies值
        /// </summary>
        /// <param name="key">该Cookie的键</param>
        /// <param name="value">该Cookie的值</param>
        /// <param name="endDate">该Cookie的过期日期</param>
        public static void SetCookie(string key, string value, DateTime endDate)
        {
            SetCookie(key, value, endDate, "/");
        }

        /// <summary>
        /// 设置客户端Cookies值
        /// </summary>
        /// <param name="key">该Cookie的键</param>
        /// <param name="value">该Cookie的值</param>
        /// <param name="endDate">该Cookie的过期日期</param>
        /// <param name="path">该Cookie的作用路径</param>
        public static void SetCookie(string key, string value, DateTime endDate, string path)
        {
            var cookie = new HttpCookie(Config.SitePrefix + key, value)
                {
                    Expires = endDate,
                    Path = path
                };
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 取客户端Cookies值
        /// </summary>
        /// <param name="key">该Cookie的键</param>
        /// <returns>该Cookie的值</returns>
        public static string GetCookie(string key)
        {
            var value = string.Empty;
            var cookie = HttpContext.Current.Request.Cookies[Config.SitePrefix + key];
            if (cookie != null)
                value = cookie.Value;
            return value;
        }
        /// <summary>
        /// 清除客户端Cookies值
        /// </summary>
        /// <param name="key">该Cookie的键</param>
        public static void RemoveCookie(string key)
        {
            var cookie = HttpContext.Current.Request.Cookies[Config.SitePrefix + key];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-7);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }
}
